package com.example.project.controller.project;

import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.project.core.controller.BaseController;
import com.example.project.core.domain.AjaxResult;
import com.example.project.entity.Issue;
import com.example.project.service.IssueSimilarityService;

/**
 * 需求相似度控制器
 */
@Controller
@RequestMapping("/project/issue/similarity")
public class IssueSimilarityController extends BaseController {

    @Autowired
    private IssueSimilarityService similarityService;
    
    /**
     * 查找相似需求
     */
    @RequiresPermissions("project:issue:view")
    @GetMapping("/similar/{issueId}")
    @ResponseBody
    public AjaxResult findSimilarIssues(
            @PathVariable Long issueId,
            @RequestParam(defaultValue = "5") Integer limit) {
        try {
            List<Issue> similarIssues = similarityService.findSimilarIssues(issueId, limit);
            similarIssues = issueService.convertToIssuesList(similarIssues);
            return success(similarIssues);
        } catch (Exception e) {
            return error("查找相似需求失败: " + e.getMessage());
        }
    }
    
    /**
     * 基于关键词查找相似需求
     */
    @RequiresPermissions("project:issue:view")
    @GetMapping("/search")
    @ResponseBody
    public AjaxResult searchSimilarIssues(
            @RequestParam String keywords,
            @RequestParam(required = false) Long projectId,
            @RequestParam(defaultValue = "10") Integer limit) {
        try {
            List<Issue> similarIssues = similarityService.findSimilarIssuesByKeywords(keywords, projectId, limit);
            return success(similarIssues);
        } catch (Exception e) {
            return error("基于关键词查找需求失败: " + e.getMessage());
        }
    }
    
    /**
     * 计算并缓存需求相似度
     */
    @RequiresPermissions("project:issue:edit")
    @PostMapping("/calculate/{issueId}")
    @ResponseBody
    public AjaxResult calculateSimilarities(@PathVariable Long issueId) {
        try {
            similarityService.calculateAndCacheSimilarities(issueId);
            return success("相似度计算成功");
        } catch (Exception e) {
            return error("相似度计算失败: " + e.getMessage());
        }
    }
    
    /**
     * 清除需求相似度缓存
     */
    @RequiresPermissions("project:issue:edit")
    @PostMapping("/clear/{issueId}")
    @ResponseBody
    public AjaxResult clearSimilarityCache(@PathVariable Long issueId) {
        try {
            similarityService.clearSimilarityCache(issueId);
            return success("缓存清除成功");
        } catch (Exception e) {
            return error("缓存清除失败: " + e.getMessage());
        }
    }
}